from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView, TemplateView, UpdateView, ListView, DeleteView

from assets.models import GitCode
from configs.forms import GitCodeForm
from configs.models import ConfTemplate


class CodeRepCreateView(CreateView):
    """代码仓库添加"""
    template_name = "configs/code_rep_create.html"
    form_class = GitCodeForm
    model = GitCode
    success_url = "/"


class CodeRepListView(TemplateView):
    """代码仓库列表"""
    template_name = "configs/code_rep_list.html"
    form_class = GitCodeForm
    model = GitCode
    success_url = "/"

    def get_context_data(self, **kwargs):
        gitcode_obj_list = GitCode.objects.all()

        context = {
            "gitcode_obj_list": gitcode_obj_list
        }

        kwargs.update(context)
        return super().get_context_data(**kwargs)


class CodeRepUpdateView(UpdateView):
    """代码仓库更新"""
    template_name = "configs/code_rep_update.html"
    success_url = reverse_lazy("code_rep_list")
    model = GitCode
    form_class = GitCodeForm


class SaltConfIndexView(TemplateView):
    """
    配置文件index
    """
    template_name = 'configs/salt_conf_index.html'


class ConfCreateView(CreateView):
    """添加配置文件"""
    template_name = 'configs/conf_create.html'
    model = ConfTemplate
    fields = ("name", "init_file", "template_file")
    success_url = '/'


class ConfListView(ListView):
    template_name = 'configs/conf_list.html'
    queryset = ConfTemplate.objects.all()
    context_object_name = 'conf_list'


class ConfDetailView(DeleteView):
    template_name = 'configs/conf_detail.html'
    model = ConfTemplate
    context_object_name = 'conf_item'



class ConfUpdateView(UpdateView):
    template_name = 'configs/conf_update.html'
    model = ConfTemplate
    context_object_name = 'conf_item'

